<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-20
 * Time: 下午7:06
 * To change this template use File | Settings | File Templates.
 */
class order extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        if ( !$this->AdminIsLogin() ) {
            $this->load->helper('url');
            redirect('/administrator/admin_login/index');
        }
    }

    public function index()
    {
        $this->load->view('administrator/order');
    }

    public function search()
    {
        //echo '<pre>';print_r($_REQUEST);exit;
        $Limit = 20;
        $currentPage = $this->uri->segment(4, 1);
        $offset = ($currentPage - 1) * $Limit;

        $startTime = $this->input->get_post('start_time');
        $endTime = $this->input->get_post('end_time');
        $passport = $this->input->get_post('passport');
        $status = $this->input->get_post('status');
        $channel = $this->input->get_post('channel');
        $export = $this->input->get_post('export');

        $where = array();

        if ( !empty ($startTime) && !empty ($endTime)) {
            $where['create_time >='] = $startTime;
            $where['create_time <='] = $endTime;
        }
        if ( !empty ($passport) ) {
            $where['passport'] = $passport;
        }
        if ( !empty ($status) ) {
            if ($status == '1') {
                $where['order_flag'] = '1';
                $where['exchange_flag'] = '1';
            }
            if ($status == '2') {
                $where['order_flag !='] = '1';
                $where['exchange_flag!='] = '1';
            }
        }
        if ( !empty ($channel) ) {
            $where['pay_service'] = $channel;
        }


        $this->load->model('model_order', 'order');
        $this->order->getOrderList();

        $totalNum = $this->order->getOrderCount($where);
        $data = $this->order->getOrderList($Limit, $offset, $where);//$this->design->getDesignList($Limit, $offset);

        $this->load->library('pagination');
        $config['base_url'] = site_url() . '/administrator/order/search/';
        $config['total_rows'] = $totalNum;
        $config['per_page'] = $Limit;
        $config['num_links'] = 10;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['suffix'] = '?start_time='.$startTime.'&end_time='.$endTime.'&passport='.$passport.'&status='.$status.'&channel='.$channel;
        $config['anchor_class'] = 'class="number"';
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();

        $keywords = array(
            'start_time' => $startTime,
            'end_time' => $endTime,
            'passport' => $passport,
            'status' => $status,
            'channel' => $channel,
        );
        if ($export) {
            $strData = "订单ID,用户名,金额,支付渠道,订单状态,创建时间\n\r";
            foreach ($data as $v) {
                $strData .= $v['order_sn'].','.$v['passport'].','.$v['price'].',';

                switch ($v['pay_service']) {
                     case 'wsyh': $ps = '网上银行';break;
                     case 'alipay': $ps = '支付宝';break;
                     case 'yeepay': $ps = '易宝';break;
                     case '99bill': $ps = '快钱';break;
                     case 'szx': $ps = '神州行';break;
                     case 'game_card': $ps = '游戏点卡';break;
                     default:$ps = '网上银行';
                }
                $strData .= $ps;
                switch ($v['bank_code']) {
                     case 'ICBC-NET-B2C': $bcs = '工商银行'; break;
                     case 'CCB-NET-B2C': $bcs = '建设银行'; break;
                     case 'BOC-NET-B2C': $bcs = '中国银行'; break;
                     case 'ECITIC-NET-B2C': $bcs = '中信银行'; break;
                     case 'CMBCHINA-NET-B2C': $bcs = '招商银行'; break;
                     case 'ABC-NET-B2C': $bcs = '农业银行'; break;
                     case 'SPDB-NET-B2C': $bcs = '浦发银行'; break;
                     case 'SDB-NET-B2C': $bcs = '深圳发展银行'; break;
                     case 'C': $bcs = '盛大一卡通'; break;
                     case 'N': $bcs = '搜狐一卡通'; break;
                     case 'M': $bcs = '网易一卡通'; break;
                     case 'D': $bcs = '征途一卡通'; break;
                     default: $bcs = '';
                 }
                $strData .= empty ($bcs) ? '' : '--'.$bcs;

                switch ($v['order_flag']) {
                    case '0': $ofv = '付款初始'; break;
                    case '1': $ofv = '付款成功'; break;
                    case '2': $ofv = '付款失败'; break;
                }
                $strData .= ','.$ofv.'--';
               switch ($v['exchange_flag']) {
                   case '0': $efv = '兑换初始'; break;
                   case '1': $efv = '兑换成功'; break;
                   case '2': $efv = '兑换失败'; break;
               }$strData .= $efv;

                $strData .= ','.$v['create_time']."\n\r";
            }
//echo $strData;exit;
            ob_start();
            header("Content-Type: application/octet-stream ; charset=UTF-8");
            header("Content-Disposition: attachment; filename=export.csv");
            echo $strData;
            ob_end_flush();
        } else {
            $this->load->view('/administrator/order', array('data' => $data, 'page_html' => $pageHtml, 'keywords' => $keywords));
        }
    }
}
